package com.yingside.gui.book;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class BookMainFrame extends JFrame {

    private LovoTable table;
    private LovoButton btnAdd,btnDel,btnEdit,btnQuery;
    private IBookManager bookManager = new BookManagerImpl();
    private BookAddFrame bookAddFrame;
    private BookEditFrame bookEditFrame;

    public LovoButton getBtnAdd() {
        return btnAdd;
    }

    public void setBtnAdd(LovoButton btnAdd) {
        this.btnAdd = btnAdd;
    }

    public BookMainFrame(){
        this.setTitle("LovoTable窗口");
        this.setBounds(300, 300, 400, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //最好把界面设置为自定义布局
        this.setLayout(null);

        initTable();
        initButton();

        initData();
    }

    /**
     * 初始化table
     */
    public void initTable(){
        table = new LovoTable(this,
                new String[]{"编号","书名","作者","价格"},
                new String[]{"id","bookName","author","price"},
                "id");
        table.setSizeAndLocation(0,0,400,200);
    }

    /**
     * 初始化按钮
     */
    public void initButton(){
        btnAdd = new LovoButton("新增",5,230,this);
        btnDel = new LovoButton("删除",105,230,this);
        btnEdit = new LovoButton("修改",205,230,this);
        btnQuery = new LovoButton("查询",305,230,this);

        //新增操作
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                bookAddFrame = new BookAddFrame(BookMainFrame.this);
                bookAddFrame.setVisible(true);
                btnAdd.setEnabled(false);
            }
        });

        //删除操作
        btnDel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int key = table.getKey();

                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选中一行再进行删除！");
                    return;
                }

                int n = JOptionPane.showConfirmDialog(null,"你确定要删除吗?","删除确认",JOptionPane.OK_CANCEL_OPTION);
                if (n == 0){
                    boolean flag = bookManager.delete(key);
                    if(flag){
                        JOptionPane.showMessageDialog(null,"删除成功！");
                        initData();
                    }
                    else{
                        JOptionPane.showMessageDialog(null,"删除失败！");
                    }
                }
            }
        });

        //修改操作
        btnEdit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int key = table.getKey();

                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选中一行再进行操作");
                    return;
                }



                bookEditFrame = new BookEditFrame(BookMainFrame.this, key);
                bookEditFrame.setVisible(true);
            }
        });
    }

    /**
     * 给table赋值
     */
    public void initData(){
        // todo 需要给集合赋值
        List<Book> list = bookManager.list();
        table.updateLovoTable(list);
    }

    public static void main(String[] args) {
        BookMainFrame bookMainFrame = new BookMainFrame();
        bookMainFrame.setVisible(true);
    }
}
